package com.smile.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dtflys.forest.annotation.Post;
import com.smile.commons.page.R;
import com.smile.convert.MenuTreeConverter;
import com.smile.model.dto.request.GetMenusByModuleTypeRequest;
import com.smile.model.response.response.GetMenusByRoleIdResponseDTO;
import com.smile.model.response.GetMenusByRoleIdResponse;
import com.smile.model.response.ShowMenuListResponse;
import com.smile.model.response.response.ShowMenuListResponseDto;
import com.smile.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 显示菜单列表
 *
 * @author 宁海博
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @PostMapping(value = "/list")
    public R<List<ShowMenuListResponse>> list() {
        List<ShowMenuListResponseDto> menuList = menuService.getMenuList();
        List<ShowMenuListResponse> showMenuListResponse = MenuTreeConverter.convertListToResponse(menuList);
        return R.ok(showMenuListResponse);
    }


    /**
     * 获取菜单列表
     *
     * @return
     */
    @PostMapping(value = "/listAll", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<List<ShowMenuListResponse>> listAll() {
        List<ShowMenuListResponseDto> menuList = menuService.getMenuListAll();
        return R.ok(BeanUtil.copyToList(menuList, ShowMenuListResponse.class));
    }

    @PostMapping(value = "/getMenusByRoleId", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<List<GetMenusByRoleIdResponse>> getMenusByRoleId(@RequestBody Long id) {
        List<GetMenusByRoleIdResponseDTO> list = menuService.getMenusByRoleId(id);
        return R.ok(BeanUtil.copyToList(list, GetMenusByRoleIdResponse.class));
    }

    @PostMapping(value = "/getMenusByModuleType")
    public R<List<ShowMenuListResponse>> getMenusByModuleType(@RequestBody GetMenusByModuleTypeRequest request) {
        List<ShowMenuListResponseDto> MenuList = menuService.getMenusByModuleType(request);
        List<ShowMenuListResponse> showMenuListResponse = MenuTreeConverter.convertListToResponse(MenuList);
        return R.ok(showMenuListResponse);
    }

    @GetMapping(value = "/getMenusByModuleTypeAll")
    public R<List<ShowMenuListResponse>> getMenusByModuleTypeAll() {
        List<ShowMenuListResponseDto> MenuList = menuService.getMenusByModuleTypeAll();
        List<ShowMenuListResponse> showMenuListResponse = MenuTreeConverter.convertListToResponse(MenuList);
        return R.ok(showMenuListResponse);
    }

    @GetMapping(value = "/getModules")
    public R<List<Integer>> getModules() {
        return R.ok(menuService.getModules());
    }
}
